package com.kmxd.ams.controller.logs;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.logs.ILogSystemSvc;
import com.kmxd.ams.client.logs.dto.LogSystemAddDTO;
import com.kmxd.ams.client.logs.dto.LogSystemQueryDTO;
import com.kmxd.ams.client.logs.vo.LogSystemListVO;
import com.kmxd.ams.client.logs.vo.LogSystemVO;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统操作日志 前端控制器
 *
 * @author ZuoLG
 * @since 2022-09-06
 */
@RestController
@Tag(name = "系统操作日志")
@RequestMapping("/logs/logSystem")
@AllArgsConstructor
public class LogSystemCtl {

  private ILogSystemSvc logSystemService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @AutoLog(module = "系统操作日志", title = "分页查询")
  public R<Page<LogSystemListVO>> selectPage(Page reqPage, LogSystemQueryDTO req) {
    return R.success(logSystemService.selectPage(reqPage, req));
  }

  /** 列表查询 */
  @Operation(summary = "列表查询")
  @GetMapping("/selectList")
  @AutoLog(module = "系统操作日志", title = "列表查询")
  public R<List<LogSystemListVO>> selectList(LogSystemQueryDTO req) {
    return R.success(logSystemService.selectList(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @SaCheckPermission("logs:logSystem:add")
  @AutoLog(module = "系统操作日志", title = "新增")
  public R<Void> add(@Validated @RequestBody LogSystemAddDTO req) {
    logSystemService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @SaCheckPermission("logs:logSystem:edit")
  @AutoLog(module = "系统操作日志", title = "修改")
  public R<Void> edit(@Validated @RequestBody LogSystemAddDTO req) {
    logSystemService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @AutoLog(module = "系统操作日志", title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    logSystemService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(module = "系统操作日志", title = "查看")
  public R<LogSystemVO> view(@RequestParam Long id) {
    return R.success(logSystemService.view(id));
  }
  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @SaCheckPermission("logs:logSystem:export")
  @AutoLog(module = "系统操作日志", title = "导出")
  @SneakyThrows
  public void export(LogSystemQueryDTO req, HttpServletResponse response) {
    String filename = "系统操作日志列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(LogSystemListVO.class)
        .sheet("系统操作日志列表")
        .doWrite(logSystemService.selectList(req));
  }
}
